package Coursework;

import java.util.Date;
import java.util.Scanner;

public class ToDoListOperations {
	private static final Scanner SCANNER = new Scanner(System.in);
	private static ToDoList readFromFile(){
		Util util = new Util();
		ToDoList itemList = null;
		try{
			itemList = util.getAllFromFile();
			System.out.println("Read list from file successfully!");
		}catch(Exception e){
			if(itemList == null){
				itemList = new ToDoList();
			}
			System.out.println("Something wrong when reading list from file!");
		}
		return itemList;
	}
	
	private static void printList(ToDoList itemList){
		System.out.println();
		for(ToDoListItem item : itemList.getTodoList()){
			System.out.println(item);
		}
		System.out.println();
		System.out.println("Print list successfully!");
	}
	
	private static void printListByOrder(ToDoList itemList){
		System.out.println();
		for(ToDoListItem item : itemList.getListOrderByPriorityAndDueDate()){
			System.out.println(item);
		}
		System.out.println();
		System.out.println("Print list order by priority and due date successfully!");
	}
	
	private static void addItem(ToDoList itemList){
		Util util = new Util();
		System.out.println();
		ToDoListItem item = util.getAnItem(SCANNER);
		if(item != null){
			item.setId(itemList.getNextId());
			itemList.add(item);
			System.out.println("Add item successfully!");
		}else{
			System.out.println("Add item failed! You must input the item in correct format!");
		}
		System.out.println();
	}
	
	private static void removeItem(ToDoList itemList){
		System.out.println();
		Util util = new Util();
		for(ToDoListItem todolistitem : itemList.getTodoList()){
			System.out.println(todolistitem);
		}
		System.out.print("Please input the id of the item you want to remove: ");
		int id = util.getInt(SCANNER);
		if(id>=0&&id<itemList.size()){
			int i=0;
			for(i=0;i<itemList.size();i++){
				if(itemList.getTodoList().get(i).getId() == id){
					break;
				}
			}
			itemList.getTodoList().remove(i);
			System.out.println("Remove item successfully!");
		}else{
			System.out.println("Remove item failed!Please input correct id!");
		}
		System.out.println();
	}
	
	private static void modifyItem(ToDoList itemList){
		System.out.println();
		Util util = new Util();
		for(ToDoListItem todolistitem : itemList.getTodoList()){
			System.out.println(todolistitem);
		}
		System.out.print("Please input the id of the item you want to modify: ");
		int itemid = util.getInt(SCANNER);
		if(itemid>=0&&itemid<itemList.size()){
			System.out.println("Which property you want to modify?");
			System.out.println("1.Priority");
			System.out.println("2.Due Date");
			int v = util.getInt(SCANNER);
			if(v == 1){
				System.out.print("Input new priority: ");
				int p = util.getInt(SCANNER);
				if(p==0){
					System.out.println("Wrong input!");
				}else{
					int i=0;
					for(i=0;i<itemList.size();i++){
						if(itemList.getTodoList().get(i).getId() == itemid){
							break;
						}
					}
					ToDoListItem modifyItem = itemList.getTodoList().get(i);
					modifyItem.setPriority(p);
					itemList.getTodoList().remove(i);
					itemList.getTodoList().add(i, modifyItem);
					System.out.println("Modify item successfully!");
				}
			}else
			if(v == 2){
				System.out.print("Input new due date(yyyy-MM-dd): ");
				Date d = util.getDate(SCANNER);
				if(d==null){
					System.out.println("Wrong input!");
				}else{
					int i=0;
					for(i=0;i<itemList.size();i++){
						if(itemList.getTodoList().get(i).getId() == itemid){
							break;
						}
					}
					ToDoListItem modifyItem = itemList.getTodoList().get(i);
					modifyItem.setDueDate(d);
					itemList.getTodoList().remove(i);
					itemList.getTodoList().add(i, modifyItem);
					System.out.println("Modify item successfully!");
				}
			}else{
				System.out.println("Wrong input!");
			}
			
		}else{
			System.out.println("Modify item failed!Please input correct id!");
		}
		System.out.println();
	}
	
	private static void saveToFile(ToDoList itemList){
		Util util = new Util();
		try{
			util.saveToFile(itemList);
			System.out.println("Save to file successfully!");
		}catch(Exception e){
			System.out.println("Something wrong when saving list file!");
		}
	}
	
	private static void exit(ToDoList itemList){
		Util util = new Util();
		try{
			util.saveToFile(itemList);
			SCANNER.close();
			System.exit(0);
		}catch(Exception e){
		}
	}
	
	public static void main(String args[]){
		Util util = new Util();
		ToDoList itemList = null;
		try{
			itemList = util.getAllFromFile();
		}catch(Exception e){
			itemList = new ToDoList();
		}
		
		while(true){
			util.printMenu();
			int cmd = util.getInt(SCANNER);
			switch(cmd){
			case 1:
				itemList = readFromFile();
				break;
			case 2:
				printList(itemList);
				break;
			case 3:
				printListByOrder(itemList);
				break;
			case 4:
				addItem(itemList);
				break;
			case 5:
				removeItem(itemList);
				break;
			case 6:
				modifyItem(itemList);
				break;
			case 7:
				saveToFile(itemList);
				break;
			case 8:
				exit(itemList);
			}
		}
	}
}
